package com.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.dao.UserDao;
import com.model.User;

@Repository("userdao")
@Transactional
public class UserDaoImpl implements UserDao {
	
	@Resource SessionFactory sessionFactory;

	public void add(User user) {		
		sessionFactory.getCurrentSession().persist(user);
	}

	public void delete(String username) {
		sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().get(User.class, username));
	}

	public void change(User user,String username) {	
		sessionFactory.getCurrentSession().delete(find(username));
		sessionFactory.getCurrentSession().persist(user);	
	}

	public User find(String username) {
		return (User) sessionFactory.getCurrentSession().get(User.class, username);
	}

	@SuppressWarnings("unchecked")
	public List<User> list() {
		return sessionFactory.getCurrentSession().createQuery("from User").list();
	}
}
